﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Fares
{
    public partial class OptiuniRapoarteFisaMagazieFrm : FrmCard
    {
        public int LunaCurenta = 0;
        public int AnulCurent = 0;
        private String SerieNumerica = string.Empty;
        private Int16 tipRaport = 0;
        private Boolean ParteneriProprii = true;

        public OptiuniRapoarteFisaMagazieFrm(ModuleClass mc,Int16 _tiprap):
            base(mc)
        {
            tipRaport = _tiprap;
            InitializeComponent();
            workConnString = mc.workConnectionString;
            switch(tipRaport)
            {
                case 1:
                    Text = "Optiuni rapoarte - Fisa Magazie";
                    break;
                default:
                    Text = "Optiuni rapoarte";
                    break;
            }
            PanaLaDTP.Format = DateTimePickerFormat.Custom;
            PanaLaDTP.CustomFormat = "dd.MM.yyyy";
        }

        private void OptiuniRapoarteFrm_Load(object sender, EventArgs e)
        {
             SqlConnection con = DeschideConexiune(workConnString);

            #region Determinare luna,an curent
            Object ret = GetSetupValue("FaresSetup", "IDSerie", ModuleInfo.CodUnitate, con);
            String val = String.Empty;
            if (ret != null && ret != DBNull.Value)
                val = ret.ToString();
            else
            {
                while (ret == null)
                {
                    MessageBox.Show("Modulul Parteneri nu a fost configurat. \nVa rugam sa-l configurati!",
                            "Eroare",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand);
                    FaresSetupFrm frm = new FaresSetupFrm(this.ModuleInfo);
                    frm.ShowDialog();
                    ret = GetSetupValue("FaresSetup", "IDSerie", ModuleInfo.CodUnitate, con);
                }
            }
            SerieNumerica = val;
            Object retObj = GetSetupValue("FaresSetup", "LunaCurenta", ModuleInfo.CodUnitate, con);
            if (retObj != null && retObj != DBNull.Value)
                LunaCurenta = Convert.ToInt32(retObj);
            retObj = GetSetupValue("FaresSetup", "AnCurent", ModuleInfo.CodUnitate, con);
            if (retObj != null && retObj != DBNull.Value)
                AnulCurent = Convert.ToInt32(retObj);

            retObj = GetSetupValue("FaresSetup", "ParteneriProprii", ModuleInfo.CodUnitate, con);
            if (retObj != null && retObj != DBNull.Value)
                ParteneriProprii = Convert.ToBoolean(retObj);

            #endregion
            DateTime inceputLuna = new DateTime(AnulCurent, LunaCurenta, 1);
            DateTime sfarsitLuna = inceputLuna.AddMonths(1).AddDays(-1);
            PanaLaDTP.Value = sfarsitLuna;

            switch (tipRaport)
            {
                case 1:
                    break;

                default:
                    break;
            }
            this.articoleTableAdapter.Connection = con;
            this.articoleTableAdapter.Fill(this.eSRPDataSetListeStocuri.Articole, ModuleInfo.CodUnitate);

            AddButton("Genereaza[F9]", global::ERPStudio.Properties.Resources._001_07, Genereaza, ToolStripItemAlignment.Right, false);
   
            HandleSalvare += Genereaza;
            InchideConexiune(con);
        }

        private void Genereaza(object sender, EventArgs e)
        {

            String filtruArt = string.Empty;
 
 
            String filtru = " [CodUnitate] = " + ModuleInfo.CodUnitate.ToString();
            String NumeRaport = "FisaMagazie.rdlc";

            switch(tipRaport)
            {
                case 1:
                    NumeRaport = "FisaMagazie.rdlc";
                    break;
                default:
                    return;

            }

            if (articoleGB.Visible && articolRB.Checked)
            {
                filtruArt = articolCB.SelectedValue.ToString();
            }
            if (articoleGB.Visible && articoleRB.Checked)
            {
                for (Int16 i = 0; i < articoleSelectateBindingSource.Count; i++)
                {
                    if (filtruArt != String.Empty)
                        filtruArt += ",";
                    filtruArt += ((FurnizoriArticoleSelectate)articoleSelectateBindingSource.List[i]).Tag.ToString();
                }
            }
            SqlConnection con = DeschideConexiune(workConnString);
            PreviewRaportFrm frm = new PreviewRaportFrm(NumeRaport, null, ModuleInfo);
            switch (tipRaport)
            {
                case 1:
                    eSRPDataSetListeStocuri.EnforceConstraints = false;
                    this.fares_FiltreazaListaFisaMagazieTableAdapter.Connection = DeschideConexiune(workConnString);
                    this.fares_FiltreazaListaFisaMagazieTableAdapter.Fill(this.eSRPDataSetListeStocuri.fares_FiltreazaListaFisaMagazie,
                    filtruArt, PanaLaDTP.Value,ModuleInfo.CodUnitate);
                    this.balconSetupTableAdapter.Connection = this.fares_FiltreazaListaFisaMagazieTableAdapter.Connection;
                    this.balconSetupTableAdapter.Fill(this.eSRPDataSetListeStocuri.BalconSetup,ModuleInfo.CodUnitate);
                    break;
                default:
                    break;
            }
        
            frm.AddDataSource("BalconSetup", balconSetupBindingSource);
            switch (tipRaport)
            {
                case 1:
                    frm.AddParameter("Linia1", "Lista documente", true);
                    frm.AddParameter("Centralizata", centralizataCB.Checked?"True":"False", true);
                    frm.AddDataSource("fares_FiltreazaListaFisaMagazie",fares_FiltreazaListaFisaMagazieBindingSource);
                break;
                default:
                    return;

            }
            
            

            frm.AddParameter("Linia2", "La data " +
                PanaLaDTP.Value.Day.ToString() + "." + PanaLaDTP.Value.Month.ToString() + "." + PanaLaDTP.Value.Year.ToString(), true);
            
            frm.MdiParent = MdiParent;
            
            frm.Show();
            InchideConexiune(con);
        
        }

        private void addArt_Click(object sender, EventArgs e)
        {
            articoleSelectateBindingSource.Add(new FurnizoriArticoleSelectate(articolCB.Text,
                Convert.ToInt16(articolCB.SelectedValue)));
            articoleRB.Checked = true;
        }

        private void delArt_Click(object sender, EventArgs e)
        {
            articoleSelectateBindingSource.RemoveCurrent();
        }
     
    }

   
    
}
